const app = getApp();
//引入插件：微信同声传译
const plugin = requirePlugin('WechatSI');
Page({
  data: {
    id:"",
    grade:"",
    description:"",
    xiaoxun:"",
    tximg:"",
    floor:"",
    longitude:null,
    latitude:null,
    carouselImg:"", // 景点轮播图
    isCanComment:false, // 是否开启评论功能
    title: "",
    isCanVideo:false,
    xz_longitude:0,
    xz_latitude:0,
    src:'', // 文字装语音地址路径
    touched:0, // 语音播放控制
    videosrc:"",// 视频地址
    imgUrl:app.globalData.imgUrl,
  }, 
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {    
    var that=this;
    wx.setNavigationBarTitle({
      title: options.title+"简介"
    })

    wx.request({
      url: app.globalData.Url+`/wxstudent/wxsceneryapplets`,
      data:{ },
      method: 'POST', 
      header:{'content-type':'application/x-www-form-urlencoded'},
      success: function (req) {
        var list=req.data
        that.setData({
          // indexPar:list[0],
          isCanComment:list[0].mycomment=='开',
        })
      },
    })

    //获取目前位置
    wx.getLocation({
      "type":"gcj02",
      "altitude":"true",
      success:function(res){
        that.setData({
          xz_latitude:Number(res.latitude),
          xz_longitude:Number(res.longitude),        
        });
       },
     fail:function(){
       wx.showToast({
         title: '网络繁忙！',
         icon:'loading',
         duration:2000
       });
     },
    }) 

    wx.request({
      url: app.globalData.Url+`/wxstudent/wxGetInfoById`,
      data:{
        id:options.id,
        type:options.type,
      },
      method: 'POST', 
      header:{'content-type':'application/x-www-form-urlencoded'},
      success: function (req) {
        var data=req.data
        that.setData({
          id:data.id,
          grade:options.type==1 ? 0 : data.grade,
          title: options.title.length>8?options.title.substr(0, 5)+"...":options.title,
          description: data.description,
          xiaoxun:options.type==1 ? data.xiaoxun : "",
          floor:data.floor,
          tximg:data.tximg,
          longitude:data.longitude,
          latitude: data.latitude,
          isCanVideo:options.type == 1 ? true :false,
          carouselImg:options.type == 1 ? "" :data.img,
          videosrc:data.videosrc,
        })
      },
    })

    
  },
  onReady(e) {
    //创建内部 audio 上下文 InnerAudioContext 对象。
    this.innerAudioContext = wx.createInnerAudioContext();
    this.innerAudioContext.onError(function (res) {
      console.log(res);
      wx.showToast({
        title: '语音播放失败',
        icon: 'none',
      })
    }) 
  },

  // 文字转语音
  wordYun:function (e) {
    if(this.data.touched==0){
      var that = this;
      var content = this.data.description;
      plugin.textToSpeech({
        lang: "zh_CN",
        tts: true,
        content: content,
        success: function (res) {
          that.setData({
            src: res.filename
          })
          that.yuyinPlay();
        },
        fail: function (res) {
          console.log("fail tts", res)
        }
      })  
    }else{
      this.end()
    }
  },
  
  //播放语音
  yuyinPlay: function (e) {
    if (this.data.src == '') {
      console.log(暂无语音);
      return;
    }
    this.innerAudioContext.src = this.data.src //设置音频地址
    this.innerAudioContext.play(); //播放音频
    var that = this;
    that.setData({
      touched:this.data.touched=this.data.touched+1
    })
  },
 
  // 结束语音
  end: function (e) {
    this.innerAudioContext.pause();//暂停音频
    this.setData({
      touched:this.data.touched=this.data.touched-1
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    var title, path;
    title = this.data.title + ' - ' + '校园导览'
    path = "/pages/map/details?tid=" + this.data.tid + "&bid=" + this.data.bid
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    return {
      title: title,
      path: path,
      imageUrl: app.imgCDN + this.data.carouselImg,
      success: function (res) {
        // 转发成功
      },
      fail: function (res) {
        // 转发失败
      }
    }
  },
})